<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用Map类型控制表单提交</title>
</head>
<body>
    <form action="" onsubmit="return post()">
        <input type="checkbox" name="xy" error="请勾选同意协议" id="xy"><label for="xy">同意协议</label>
        <input type="checkbox" name="xs" error="请确定是否是学生" id="xs"><label for="xs">我是学生</label>
        <input type="submit">
    <script>
        function post() {
            let map = new Map();
            document.querySelectorAll("[error]").forEach(item => {
                map.set(item,
                {
                    error:item.getAttribute("error"),
                    status:item.checked
                });
            });

            return [...map].every(([element,config]) => {
                config.status || alert(config.error);
                return config.status;
            });
        }
        
    </script>
</body>
</html>